package com.zdd.chap4a.controller;

import com.zdd.chap4a.entity.Student;
import com.zdd.chap4a.mapper.StudentMapper;
import com.zdd.chap4a.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/toupdatestudent/{id}")
    public ModelAndView toUpdate(@PathVariable("id")int id){
        Student student=studentService.findStudent(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("student",student);
        mv.setViewName("updateStudent");
        return mv;
    }

    @GetMapping("/updatestu")
    public ModelAndView updateStudent(Student student){
        studentService.updateStudent(student);
        student=studentService.findStudent(student.getId());
        ModelAndView mv=new ModelAndView();
        mv.addObject("student",student);
        mv.setViewName("updateStudent");
        return mv;
    }

    @GetMapping("/students/{id}")
    public ModelAndView findStudentById(@PathVariable("id") int id){
        Student student=studentService.findStudentById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("student",student);
        mv.setViewName("student");
        return mv;
    }
}
